<template>
  <div class="page-content">
    <div v-wechat-title="$route.meta.title"></div>
    <mt-field label="申请人手机"
              placeholder="请输入申请人手机"
              class="must"
              type="tel"
              v-model="phone"
              @blur.native.capture="checkPhone()"></mt-field>
    <mt-field label="验证码"
              placeholder="输入验证码"
              class="must"
              v-model="code">
      <!-- <mt-button type="primary"
                 class="yzm"
                 size="small"
                 @click="getcode"
                 v-show="show">获取验证码</mt-button> -->
      <!-- <mt-button type="default"
                 class="yzm"
                 size="small"
                 v-show="!show">{{count}}s</mt-button> -->
      <!-- <span v-show="!show">222</span> -->
      <img @click="getcode()"
           :src="'data:image/png;base64,' + yzm"
           alt=""
           class="yzm">
    </mt-field>
    <mt-field label="证件类型"
              class="must"
              disabled>
      <mt-popup v-model="popupVisible"
                popup-transition="bottom"
                closeOnClickModal="true"
                position="bottom">
        <mt-picker :slots="ID_type"
                   @change="onshipChange"
                   showToolbar>
          <mt-button type="primary"
                     class="usi-btn-sure"
                     @click="popupVisible = !popupVisible"
                     size="small">确定</mt-button>
        </mt-picker>
      </mt-popup>
      <div class="my-content-list row justfy"
           @click="ShouPup">
        <input readonly
               v-model="idType" />
        <img class="arrow"
             src="../assets/images/arrow-left.png"
             alt />
      </div>
    </mt-field>
    <mt-field label="证件号码"
              class="must"
              placeholder="证件号码"
              v-model="idcard"
              @blur.native.capture="checkIDcard()"></mt-field>

    <mt-button type="primary"
               class="addcard"
               size="small"
               @click="addHealthCard">登录</mt-button>
  </div>
</template>
<script>
import "common/css/form.styl";
import wechat from '@/assets/js/wechat.js';// 跳转微信 获取权限
export default {
  data () {
    return {
      name: "", //申请人姓名
      company: "", //公司名称
      address: "", //公司地址
      phone: "", //手机号
      idcard: "", //身份证号
      idType: "",
      idTypeCode: "",
      popupVisible: false,
      code: "", //验证码
      // codeKey: "", //验证码key
      yzm: "",
      // 验证码
      show: true,
      timer: null,
      count: "",
      ID_type: [
        //证件类型
        {
          values: ["身份证", "护照", "港澳台通行证"]
        }
      ]
    };
  },
  watch: {},
  methods: {
    checkIDcard () {
      if (this.idcard) {
        if (!this.$public.isIDcard(this.idcard)) {
          this.$public.toast("身份证格式有误！");
        }
      }
    },
    checkPhone () {
      if (this.phone) {
        if (!this.$public.isPhone(this.phone)) {
          this.$public.toast("手机号码格式有误！");
        }
      }
    },
    onshipChange (picker, values) {
      this.idType = values;
      if (values[0] > values[1]) {
        picker.setSlotValue(1, values[0]);
      }
    },
    ShouPup () {
      this.popupVisible = true;
    },
    getcode () {
      this.$axios.http
        .post({
          funcName: "/fwdj/company/getPictureCode",
          params: {
            // phone: this.$public.encrypt(this.phone),
          }
        })
        .then(res => {
          this.yzm = res.data;
          // this.show = false;
          // const TIME_COUNT = 60;
          // this.codeKey = res.data.key;
          // if (!this.timer) {
          //   this.count = TIME_COUNT;
          //   this.show = false;
          //   this.timer = setInterval(() => {
          //     if (this.count > 0 && this.count <= TIME_COUNT) {
          //       this.count--;
          //     } else {
          //       this.show = true;
          //       clearInterval(this.timer);
          //       this.timer = null;
          //     }
          //   }, 1000);
          // }
        });
    },
    addHealthCard () {
      $.pubVariable.idCardType.forEach((item, i) => {
        if (item.text == this.idType[0]) {
          this.idTypeCode = item.id;
        }
      });
      if (!this.idTypeCode || !this.code) {
        this.$public.toast("信息不完整！");
      } else if (!this.$public.isIDcard(this.idcard)) {
        this.$public.toast("身份证格式有误！");
      } else if (!this.$public.isPhone(this.phone)) {
        this.$public.toast("手机号码格式有误！");
      } else {
        var _that = this;
        // 校验成功
        wechat.setRSA((RSA) => {
          this.$axios.http
            .post({
              funcName: "/fwdj/company/login",
              params: {
                sqrzjlx: this.idTypeCode,
                sqrzjhm: RSA(this.idcard, true),
                sqrsjh: RSA(this.phone, true),
                yzm: this.code,
                openId: sessionStorage.getItem("openId")
              }
            })
            .then(res => {

              if (res && res.code == "200") {
                sessionStorage.setItem("companyInfo", JSON.stringify(res.company));
                this.$router.push({ name: "login-successful" });

              } else {
                _that.$public.toast(res.msg);
              }
            });
        });


      }
    },
    // 判断是否注册
    resgister () {
      this.$axios.http
        .post({
          funcName: "/fwdj/company/isRegister",
          params: { openId: sessionStorage.getItem("openId") }
        })
        .then(res => {
          if (res && res.code == "200") { //未注册
            this.$router.push({ name: "register" });
          } else { //已注册
            console.log("已注册");
            this.getcode();
          }
        });
    }
  },
  mounted () {
    let url = window.location.href;
    wechat.wechatLogin(() => {
      this.resgister();
    }, url);
  }
};
</script>

<style lang="stylus" scoped></style>